//
//  AddClockViewController.m
//  JiuLe
//
//  Created by 黄鑫 on 16/9/1.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "AddClockViewController.h"
#import "SelectWeekViewController.h"
#import "TagViewController.h"
#import "ReminderModeViewController.h"

@interface AddClockViewController ()<SelectWeekViewControllerDelegate,TagViewControllerDelegate>

@property (weak, nonatomic) IBOutlet UIDatePicker *datePickerMain;
@property (weak, nonatomic) IBOutlet UILabel *labWeek;
@property (weak, nonatomic) IBOutlet UILabel *labTag;
@property (strong, nonatomic) NSMutableArray *arrWeek;
@property (strong, nonatomic) NSMutableArray *arrSelectWeek;
@end

@implementation AddClockViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (_clockVCType == ClockVCTypeAdd) {
        self.title = @"添加闹钟";
    }else{
        self.title = @"编辑闹钟";
    }
    [self showNavgationWithLeftTiele:@"取消" andleftBtnAction:@selector(clearAction) WithRightTiele:@"确定" andrigheBtnAction:@selector(sureAction)];
    _arrSelectWeek = [NSMutableArray array];
    if (_clockVCType == ClockVCTypeEdit) {
        _labTag.text = [self.dict objectForKey:@"clock_mark"];
        NSString *time = [self.dict objectForKey:@"clock_time"];
        NSDateFormatter *format = [[NSDateFormatter alloc] init];
        [format setDateFormat:@"HH:mm"];
        NSDate *date = [format dateFromString:time];
        [_datePickerMain setDate:date animated:YES];
        NSArray *arrCycle = [[self.dict objectForKey:@"clock_cycle"] componentsSeparatedByString:@","];
        _arrWeek = [NSMutableArray arrayWithArray:arrCycle];
        [self showSelectWeek];
        [self showWeek:_arrSelectWeek];
    }
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:YES];
}

#pragma mark - 
- (void)setAlarmClock:(NSString *)time{
     [SVProgressHUD showWithStatus:@"添加闹钟.." maskType:SVProgressHUDMaskTypeClear];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:[[NSUserDefaults standardUserDefaults] objectForKey:UserDataID] forKey:@"user_id"];
    if(_clockVCType == ClockVCTypeAdd){
        [dict setObject:@"0" forKey:@"clock_id"];
    }else{
        [dict setObject:[self.dict objectForKey:@"clock_id"] forKey:@"clock_id"];
    }
    [dict setObject:_labTag.text forKey:@"clock_mark"];
    [dict setObject:time forKey:@"clock_time"];
    [dict setObject:_arrWeek forKey:@"clock_cycle"];
    [dict setObject:@"1" forKey:@"clock_asyn"];
    if(_clockVCType == ClockVCTypeAdd){
        [dict setObject:@"1" forKey:@"isopen"];
    }else{
        [dict setObject:[self.dict objectForKey:@"isopen"] forKey:@"isopen"];
    }
    [[BaseDataRequest alloc] setAlarmClock:dict WithBlock:^(id responseObject, NSString *status, NSError *error) {
        [SVProgressHUD dismiss];
        if ([status isEqualToString:@"0"]) {
            [self clearAction];
        }else{
            if (error != nil) {
                TIPSFORVIEW(@"请检测网络设置");
            }else{
                TIPSFORVIEW([responseObject objectForKey:@"message"]);
            }
        }
    } ];
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){
        SelectWeekViewController *selectWeek = [[UIStoryboard storyboardWithName:@"PersonalViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"SelectWeekViewController"];
        selectWeek.deleaget = self;
        selectWeek.arrSelect = _arrSelectWeek;
        [self.navigationController pushViewController:selectWeek animated:YES];
    }else if(indexPath.row == 1){
        TagViewController *tag = [[UIStoryboard storyboardWithName:@"PersonalViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"TagViewController"];
        tag.deleaget = self;
        tag.strTag = _labTag.text;
        [self.navigationController pushViewController:tag animated:YES];
    }else{
        ReminderModeViewController *tag = [[UIStoryboard storyboardWithName:@"PersonalViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"ReminderModeViewController"];
        [self.navigationController pushViewController:tag animated:YES];
    }
}

#pragma mark - SelectWeekViewControllerDelegate
- (void)backSelect:(NSMutableArray *)arr;{
    [self showWeek:arr];
    _arrWeek = [NSMutableArray array];
    for (int i = 0; i < arr.count; i++) {
        NSString *str = arr[i];
        if ([str isEqualToString:@"周一"]) {
            [_arrWeek addObject:@"1"];
        }else if ([str isEqualToString:@"周二"]) {
            [_arrWeek addObject:@"2"];
        }else if ([str isEqualToString:@"周三"]) {
            [_arrWeek addObject:@"3"];
        }else if ([str isEqualToString:@"周四"]) {
            [_arrWeek addObject:@"4"];
        }else if ([str isEqualToString:@"周五"]) {
            [_arrWeek addObject:@"5"];
        }else if ([str isEqualToString:@"周六"]) {
            [_arrWeek addObject:@"6"];
        }else{
            [_arrWeek addObject:@"7"];
        }
    }
     [self showSelectWeek];
}

#pragma mark - TagViewControllerDelegate
- (void)backTagName:(NSString *)name;{
    _labTag.text = name;
}

#pragma mark - 私有方法
- (void)clearAction{
    if (_clockVCType == ClockVCTypeAdd) {
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];

    }else{
        [self.navigationController popViewControllerAnimated:YES];

    }
}

- (void)sureAction{
    if (_labWeek.text.length == 0) {
        TIPSFORVIEW(@"请设置日期");
        return;
    }
    if(_labTag.text.length == 0){
        TIPSFORVIEW(@"请设置标签");
        return;
    }
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"HH:mm"];
    NSString *time = [format stringFromDate:_datePickerMain.date];
    [self setAlarmClock:time];
}

- (void)showWeek:(NSMutableArray *)arr{
    _labWeek.text = @"";
    for (NSString *week in arr) {
        _labWeek.text = [_labWeek.text stringByAppendingFormat:@" %@",week];
    }
    if (arr.count == 7) {
        _labWeek.text = @"每天";
    }else if (arr.count == 2){
        if ([arr.firstObject isEqualToString:@"周日"] && [arr.lastObject isEqualToString:@"周六"]) {
            _labWeek.text = @"周末";
        }
    }else if (arr.count == 5){
        if ([arr.firstObject isEqualToString:@"周一"] && [arr.lastObject isEqualToString:@"周五"]) {
            _labWeek.text = @"工作日";
        }
    }
}

- (void)showSelectWeek{
    [_arrSelectWeek removeAllObjects];
    for (int i = 0; i < _arrWeek.count; i++) {
        NSString *str = _arrWeek[i];
        if ([str isEqualToString:@"1"]) {
            [_arrSelectWeek addObject:@"周一"];
        }else if ([str isEqualToString:@"2"]) {
            [_arrSelectWeek addObject:@"周二"];
        }else if ([str isEqualToString:@"3"]) {
            [_arrSelectWeek addObject:@"周三"];
        }else if ([str isEqualToString:@"4"]) {
            [_arrSelectWeek addObject:@"周四"];
        }else if ([str isEqualToString:@"5"]) {
            [_arrSelectWeek addObject:@"周五"];
        }else if ([str isEqualToString:@"6"]) {
            [_arrSelectWeek addObject:@"周六"];
        }else{
            [_arrSelectWeek addObject:@"周日"];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
